package com.nlscan.qdkit.ui.publico.art.mvp;

import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.viewbinding.ViewBinding;

import com.nlscan.qdkit.ui.publico.R;
import com.nlscan.qdkit.ui.publico.config.PublicoVariate;
import com.qing.mvpart.mvp.IPresenter;
import com.quvii.qvlib.util.QvSystemUtil;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;

/**
 * 带有标题栏的 Fragment 基类
 * Created by QING on 2018/1/5.
 */
public abstract class TitleBarBaseFragment<T extends ViewBinding, P extends IPresenter> extends BaseMvpFragment<T, P> {

    public CommonTitleBar mTitleBar;

    @Override
    public void onLayoutComplete(View view) {
        super.onLayoutComplete(view);
        mTitleBar = view.findViewById(R.id.publico_title_bar);
    }

    protected void setTitleBar(String title) {
        setTitleBar(title, R.color.publico_title_bar_bg, true);
    }

    protected void setTitleBar(String title, @ColorRes int bgColorId) {
        setTitleBar(title, bgColorId, true);
    }

    protected void setTitleBar(String title, boolean isFinish) {
        setTitleBar(title, R.color.publico_title_bar_bg, isFinish);
    }

    /**
     * 设置标题栏
     *
     * @param title     标题
     * @param bgColorId 标题栏背景色
     * @param isFinish  true表示左侧显示返回按钮
     */
    protected void setTitleBar(String title, @ColorRes int bgColorId, boolean isFinish) {

        setTitleName(title);
        setTitleBarBg(bgColorId);
        ImageButton btnLeft = mTitleBar.getLeftImageButton();
        if (isFinish) {
            btnLeft.setVisibility(View.VISIBLE);
            // [废弃]自动适配返回icon颜色
            /*
            if (ColorUtils.isLightColor(getResources().getColor(bgColorId))) {
                btnLeft.setImageResource(R.drawable.publico_selector_btn_back);
            } else {
                btnLeft.setImageResource(R.drawable.publico_selector_btn_back_white);
            }
             */
            btnLeft.setImageResource(R.drawable.publico_selector_btn_back);
            mTitleBar.setListener(new CommonTitleBar.OnTitleBarListener() {
                @Override
                public void onClicked(View v, int action, String extra) {
                    if (action == CommonTitleBar.ACTION_LEFT_BUTTON) {
                        // TODO: 2018/4/8 这里需要添加点击事件么
                        if (getActivity() != null) {
                            getActivity().onBackPressed();
                            //getFragmentManager().popBackStack();
                        }
                    }
                }
            });
        } else {
            // 隐藏左侧返回按钮
            btnLeft.setVisibility(View.GONE);
        }
    }

    protected void setTitleBar(String title, @DrawableRes int resId, View.OnClickListener listener) {
        setTitleBar(title, R.color.publico_title_bar_bg, resId, listener);
    }

    /**
     * 设置左侧按钮点击事件
     */
    protected void setTitleBar(String title, @ColorRes int bgColorId, @DrawableRes int resId, View.OnClickListener listener) {
        setTitleName(title);
        setTitleBarBg(bgColorId);
        ImageButton btnLeft = mTitleBar.getLeftImageButton();
        btnLeft.setImageResource(resId);
        btnLeft.setOnClickListener(listener);
    }

    /**
     * 设置标题
     */
    public void setTitleName(String title) {
        if (!TextUtils.isEmpty(title)) {
            TextView tvCenter = mTitleBar.getCenterTextView();
            tvCenter.setVisibility(View.VISIBLE);
            tvCenter.setText(title);
        }
    }

    /**
     * 获取标题
     */
    public String getTitleName() {
        return mTitleBar.getCenterTextView().getText().toString().trim();
    }

    /**
     * 设置标题栏背景色
     * -- 1.适配标题文字颜色
     * -- 2019-1-17  2. 适配沉浸式状态栏图标颜色模式
     */
    public void setTitleBarBg(@ColorRes int bgColorId) {
        int bgColor = getResources().getColor(bgColorId);
        mTitleBar.setBackgroundColor(bgColor);
        //TODO fillStatusBar 未暴露出来，当前默认支持沉浸式
        mTitleBar.setStatusBarColor(bgColor);  //状态栏背景色与标题栏背景色保持一致
        //如果标题栏背景色为白色，修改字体颜色为黑色，若为其他颜色，标题文字改为白色
        TextView tvCenter = mTitleBar.getCenterTextView();
        if (QvSystemUtil.isLightColor(getResources().getColor(bgColorId))) {
            tvCenter.setTextColor(Color.parseColor("#333333"));
            // 2. 适配沉浸式状态栏图标颜色模式
            mTitleBar.setStatusBarMode(CommonTitleBar.STATUS_BAR_MODE_DARK);
        } else {
            tvCenter.setTextColor(Color.WHITE);
            mTitleBar.setStatusBarMode(CommonTitleBar.STATUS_BAR_MODE_LIGHT);
        }
    }

    /**********************************
     * 左侧按钮相关 BEGIN
     ************************************/

    /**
     * CommonTitleBar 设置默认leftType，调用此函数需要将
     * leftType 设置为 ImageButton
     *
     * @param resId
     * @param listener
     */
    public void setTitleBarLeftBtn(@DrawableRes int resId, View.OnClickListener listener) {
        // [bugFix]空指针异常
        if (mTitleBar == null) return;
        ImageButton btnLeft = mTitleBar.getLeftImageButton();
        if (btnLeft == null) {
            return;
        }
        btnLeft.setImageResource(resId);
        btnLeft.setOnClickListener(listener);
    }

    /**********************************
     * 左侧按钮相关 END
     ************************************/


    /****************************************
     * 右侧按钮 BEGIN
     *****************************************/

    public void setTitleBarRightTv(String text, View.OnClickListener listener) {

        TextView tvCenter = mTitleBar.getCenterTextView();
        int tvColor = tvCenter.getCurrentTextColor();
        setTitleBarRightTv(text, tvColor, listener);
    }

    public void setTitleBarRightTv(String text, @ColorInt int tvColor, View.OnClickListener listener) {
        // [bugFix]空指针异常
        if (mTitleBar == null) return;
        TextView tvRight = mTitleBar.getRightTextView();
        if (tvRight == null) {
            return;
        }
        tvRight.setText(text);
        tvRight.setTextColor(tvColor);
        tvRight.setOnClickListener(listener);
    }

    /**
     * CommonTitleBar 设置默认rightType，调用此函数需要将
     * rightType 设置为 ImageButton
     *
     * @param resId
     * @param listener
     */
    public void setTitleBarRightBtn(@DrawableRes int resId, View.OnClickListener listener) {
        // [bugFix]空指针异常
        if (mTitleBar == null) return;
        ImageButton btnRight = mTitleBar.getRightImageButton();
        if (btnRight == null) {
            return;
        }
        btnRight.setImageResource(resId);
        btnRight.setOnClickListener(listener);
    }

    /**
     * 设置右侧按钮图标
     */
    public void setTitleBarRightBtnDrawable(@DrawableRes int resId) {
        // [bugFix]空指针异常
        if (mTitleBar == null) return;
        ImageButton btnRight = mTitleBar.getRightImageButton();
        if (btnRight == null) {
            return;
        }
        btnRight.setImageResource(resId);
    }

    /****************************************
     * 右侧按钮 END
     *****************************************/


    /**** 设置标题栏左右按钮的显隐 BEGIN ****/

    /**
     * 设置左侧按钮显隐状态
     */
    public void setLeftImageButtonVisible(boolean isShow) {
        // [bugFix]空指针异常
        if (mTitleBar == null) return;
        ImageButton btnLeft = mTitleBar.getLeftImageButton();
        if (btnLeft == null) return;
        btnLeft.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    /**
     * 设置右侧按钮显隐状态
     */
    public void setRightImageButtonVisible(boolean isShow) {
        // [bugFix]空指针异常
        if (mTitleBar == null) return;
        ImageButton btnRight = mTitleBar.getRightImageButton();
        if (btnRight == null) return;
        btnRight.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    /**** 设置标题栏左右按钮的显隐 END ****/


    /*****************
     * Get 方法 BEGIN
     *****************/

    public CommonTitleBar getTitleBar() {
        return mTitleBar;
    }

    /*****************
     * Get 方法 END
     *****************/

    @Override
    public void onResume() {
        super.onResume();
        // 非PAD模式下，预览和回放页面解除强制竖屏，切换到别的页卡再设置回来
        if (getActivity() != null && !PublicoVariate.isPadMode) {
            if (PublicoVariate.isInPreviewOrPlaybackModule) { //fix bug:13686
                getActivity().setRequestedOrientation(
                        ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
            } else {
                getActivity().setRequestedOrientation(
                        ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
        }
    }

}
